from django.shortcuts import render

# Create your views here.

from rest_framework.response import Response

from rest_framework.views import APIView

from users.models import User
from users.serializers import RegisterCreateSerializer


class RegisterUsernameCountAPIView(APIView):

    """
    填写用户名失去焦点的时候，发送ajax请求到后端，后端进行校验，返回响应
    １．接收参数，２．校验参数，３．返回响应
    请求路径：　/users/usernames/(?P<usernames>\w{5, 20})/count/
    请求方式：   GET
    选用的试图:  一级视图,不是列表视图和详情视图
    """
    def get(self, request, username):
        count = User.objects.filter(username=username).count()

        context = {
            'count': count,
            'username': username,
        }

        return Response(data=context)


class RegisterMobileCountApiView(APIView):
    """
    注册的时候,填写手机号,当失去焦点的时候,发送ajax请求,返回是否已经注册过
    请求路径:   /users/mobile/(?P<mobile>1[3-9]\d{9})/count/
    请求参数:   mobile
    """
    def get(self, request, mobile):
        count = User.objects.filter(mobile=mobile).count()
        data = {
            "count": count,
            "mobile": mobile,
        }
        return Response(data=data)


from rest_framework.generics import CreateAPIView
class RegisterCreateAPIView(CreateAPIView):
    """
    请求路径:   /users/
    请求方式:   POST
    1.注册,得要传过来6个值
    用户名,密码,短信验证码,是否同意协议,手机号,确认密码

    """

    queryset = User.objects.all()
    serializer_class = RegisterCreateSerializer



